﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Threading;
using DataRecv.tools;
using DataRecv.models.device;


namespace DataRecv.models.mq
{
    /// <summary>
    /// 设备状态巡检
    /// </summary>
    public class StatusReporter
    {
        private const int MQINTERVAL = 600000;
        private MQ m_mq;
        private Timer m_reporttimer;

        
        public StatusReporter()
        {
            try
            {

                if ((G.SystemParam.MqUri != "") && (G.SystemParam.MqTopic != ""))
                {
                    m_mq = new MQ(G.SystemParam.MqUri, G.SystemParam.MqTopic);
                    m_mq.LogEvent += M_mq_LogEvent;
                    m_mq.Start();

                    m_reporttimer = new Timer(new TimerCallback(ReportTimeFunc), null, MQINTERVAL, MQINTERVAL);

                }
            }
            catch(Exception er)
            {
                LogTool.log.Info("StatusReporter()" + er.Message);
            }
        }

        private void ReportTimeFunc(object obj)
        {
            try
            {
                if (G.Devices != null)
                {
                    foreach (Device d in G.Devices)
                    {
                        try
                        {
                            ReportMsg msg = new ReportMsg();
                            msg.roadname = d.deviceview.roadparam.roadname;
                            msg.deviceid = d.deviceview.deviceparam.sbbh;

                            if (d.state.Ping == false)//检查设备ping状态
                            {
                                //一级：网络故障3
                                msg.error1 = "3";
                                msg.error2 = "-1";
                                msg.error3 = "-1";
                                msg.occurtime = d.state.PingOccurTime.ToString("yyyy-MM-dd HH:mm:ss");
                                msg.reporttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            else if (d.state.UploadFail == true)
                            {
                                //一级：软件故障1
                                msg.error1 = "1";
                                msg.error2 = "-1";
                                msg.error3 = "-1";
                                msg.occurtime = d.state.UploadTime.ToString("yyyy-MM-dd HH:mm:ss");
                                msg.reporttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            else if (d.state.NoData == true)
                            {
                                //一级：数据异常13 二级：无数据上传1
                                msg.error1 = "13";
                                msg.error2 = "1";
                                msg.error3 = "-1";
                                msg.occurtime = d.state.NoDataTime.ToString("yyyy-MM-dd HH:mm:ss");
                                msg.reporttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            else if (d.state.IsDaTaDelay == true)
                            {
                                //一级：数据异常13 二级：数据延迟、数据质量异常2
                                msg.error1 = "13";
                                msg.error2 = "2";
                                msg.error3 = "-1";
                                msg.occurtime = d.state.LastJGSJ.ToString("yyyy-MM-dd HH:mm:ss");
                                msg.reporttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            else
                            {
                                //无故障
                                msg.error1 = "-1";
                                msg.error2 = "-1";
                                msg.error3 = "-1";
                                msg.occurtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                msg.reporttime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            }

                            //重置状态
                            d.state.UploadFail = false;

                            if (m_mq != null)
                            {
                                string rpt = msg.ToMQString();
                                bool up = m_mq.SendMQMessage(rpt);
                                if (up == true)
                                {
                                    LogTool.log.Info("状态上报：" + rpt);
                                }
                            }
                        }
                        catch(Exception er)
                        {
                            LogTool.log.Info("ReportTimeFunc1()" + er.Message);
                        }
                    }
                }
            }
            catch (Exception er)
            {
                LogTool.log.Info("ReportTimeFunc()" + er.Message);
            }
        }

        private void M_mq_LogEvent(string txt)
        {
            LogTool.log.Info(txt);
        }

        public void Dispose()
        {
            try
            {
                if (m_mq != null)
                {
                    m_mq.Close();
                }
                if (m_reporttimer != null)
                {
                    m_reporttimer.Dispose();
                }
            }
            catch { }
        }
    }
}
